<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="商品id" prop="cid">
        <el-input v-model="dataForm.cid" placeholder="商品id"></el-input>
      </el-form-item>
      <el-form-item label="优惠券名字" prop="name">
        <el-input v-model="dataForm.name" placeholder="优惠券名字"></el-input>
      </el-form-item>
      <el-form-item label="金额" prop="amount">
        <el-input-number
          :min="0"
          v-model="dataForm.amount"
          :precision="2"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="使用门槛（最小金额）" prop="threshold">
        <el-input-number
          :min="0"
          v-model="dataForm.threshold"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="有效时间" prop="useTimeRange">
        <el-date-picker
          v-model="dataForm.useTimeRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        ></el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from '@/components/upload/singleUpload'
export default {
  components: { SingleUpload },
  data() {
    return {
      visible: false,
      memberLevels: [],
      dataForm: {
        id: 0,
        cid: '',
        name: '',
        amount: '',
        threshold: '',
        starttime: '',
        endtime: '',
        useTimeRange: []
      },
      dataRule: {
        couponCid: [
          { required: true, message: '商品id不能为空', trigger: 'blur' }
        ],
        couponName: [
          { required: true, message: '优惠券名字不能为空', trigger: 'blur' }
        ],
        amount: [{ required: true, message: '金额不能为空', trigger: 'blur' }],
        threshold: [
          { required: true, message: '使用门槛不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {},
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/cart/coupon/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.cid = data.coupon.cid
              this.dataForm.name = data.coupon.name
              this.dataForm.amount = data.coupon.amount
              this.dataForm.threshold = data.coupon.threshold
              this.dataForm.starttime = data.coupon.starttime
              this.dataForm.endtime = data.coupon.endtime
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/cart/coupon/${!this.dataForm.id ? 'save' : 'update'}`
            ),
            method: 'post',
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              cid: this.dataForm.cid,
              name: this.dataForm.name,
              amount: this.dataForm.amount,
              threshold: this.dataForm.threshold,
              starttime: this.dataForm.useTimeRange[0],
              endtime: this.dataForm.useTimeRange[1]
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
